﻿using MeterialManager.Aop.ServiceProcess;
using MeterialManager.Model;
using MeterialManager.Utility;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using static System.Windows.Forms.VisualStyles.VisualStyleElement;

namespace MeterialManager
{
    public partial class FrmWarehouseManager : Form
    {
        DelBindDgv delBindDgv;
        public FrmWarehouseManager()
        {
            InitializeComponent();
        }

        private void FrmWarehouseManager_Load(object sender, EventArgs e)
        {
            BindCbx();
            BindDgv();
            delBindDgv = BindDgv;
        }

        private void BindDgv()
        {
            string warehouseCode = tbWarehouseCode.Text.Trim();
            string meterialCode = tbMeterialCode.Text.Trim();
            string recipient = tbRecipient.Text.Trim();
            string date = cbxWarehouseDate.SelectedValue.ToString();
            List<WarehouseEntry> warehouseEntries = WarehouseEntry.ListAll();
            if (cbxWarehouseDate.SelectedIndex == 0)
            {
                dgvWarehouseEntry.DataSource = warehouseEntries.FindAll(m => m.WarehouseCode.Contains(warehouseCode) && m.MeterialCode.Contains(meterialCode) && m.Recipient.Contains(recipient));
            }
            else
            {
                dgvWarehouseEntry.DataSource = warehouseEntries.FindAll(m => m.WarehouseDate.ToString("yyyy-MM-dd").Contains(date) && m.WarehouseCode.Contains(warehouseCode) && m.MeterialCode.Contains(meterialCode) && m.Recipient.Contains(recipient));
            }
        }

        private void BindCbx()
        {
            List<string> dateList = WarehouseEntry.GetDateListAll();
            dateList.Insert(0, "所有日期");
            cbxWarehouseDate.DataSource = dateList;
        }

        private void dgvWarehouseEntry_MouseDown(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Right)
            {
                tsmAdd.Visible = true;
                tsmEdit.Visible = false;
                tsmDel.Visible = false;
            }
        }

        private void dgvWarehouseEntry_CellMouseDown(object sender, DataGridViewCellMouseEventArgs e)
        {
            if (e.Button == MouseButtons.Right)
            {
                if (e.RowIndex > -1)
                {
                    dgvWarehouseEntry.Rows[e.RowIndex].Selected = true;
                    tsmAdd.Visible = false;
                    tsmEdit.Visible = true;
                    tsmDel.Visible = true;
                }
            }
        }

        private void tbMeterialCode_TextChanged(object sender, EventArgs e)
        {
            string meterialCode = tbMeterialCode.Text.Trim();
            Meterial selectMeterial = Meterial.ListAll().Find(m => m.MeterialCode == meterialCode);
            if(selectMeterial != null)
            {
                tbUnit.ReadOnly = true;
                tbMeterialName.ReadOnly = true;
                tbUnit.Text = selectMeterial.Unit;
                tbMeterialName.Text = selectMeterial.MeterialName;
            }
            else
            {
                tbUnit.ReadOnly = false;
                tbMeterialName.ReadOnly = false;
                tbUnit.Text = "";
                tbMeterialName.Text = "";
            }
                
        }

        private void btnSearch_Click(object sender, EventArgs e)
        {
            BindDgv();
        }

        private void tsmAdd_Click(object sender, EventArgs e)
        {
            FrmSetWarehouse frmSetWarehouse = new FrmSetWarehouse(delBindDgv);
            frmSetWarehouse.ShowDialog();
        }

        private void tsmEdit_Click(object sender, EventArgs e)
        {
            int id = (int)dgvWarehouseEntry.SelectedRows[0].Cells["Id"].Value;
            FrmSetWarehouse frmSetWarehouse = new FrmSetWarehouse(delBindDgv,id);
            frmSetWarehouse.ShowDialog();
        }

        private void tsmDel_Click(object sender, EventArgs e)
        {
            string warehouseCode  = dgvWarehouseEntry.SelectedRows[0].Cells["WarehouseCode"].Value.ToString();
            decimal warehouseNumber = (decimal)dgvWarehouseEntry.SelectedRows[0].Cells["WarehouseNumber"].Value;
            string meterialCode = dgvWarehouseEntry.SelectedRows[0].Cells["MeterialCode"].Value.ToString();
            WarehouseEntry warehouseEntry = new WarehouseEntry()
            {
                WarehouseCode = warehouseCode,
                MeterialCode = meterialCode,
                WarehouseNumber = -warehouseNumber,
            };
            if (MessageBox.Show("确认删除？", "是否删除？", MessageBoxButtons.OKCancel, MessageBoxIcon.Error) == DialogResult.OK)
            {
                Func<dynamic, ResponseObject<int>> func = WarehouseEntry.Delete;
                IService iService = new Service();
                var service = ServiceProxy<IService>.Create(iService);
                ResponseObject<int> responseObject = service.DoSomething(func, warehouseEntry);
                if (responseObject.code == 200)
                {
                    MessageBox.Show("操作成功！");
                }
                else
                {
                    MessageBox.Show(responseObject.msg);
                }
            }
            BindDgv();
        }
    }
}
